package com.ruoyi.common.utils.prometheus;

import com.ruoyi.common.constant.TimeRangeConstant;
import com.ruoyi.common.utils.StringUtils;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.HashMap;
import java.util.Map;

public class PromQLQueryVo {

    private String timeRange = TimeRangeConstant.RANGE_HOUR;
    private String query;
    private String type;
    private Map<String,String> labels;
    private Long start;
    private Long end;
    private Long step = 60L;
    private Long timeout;

    public Map<String,Object> buildRangeQuery(){
        Map<String,Object> query = new HashMap<>();
        LocalDateTime now = LocalDateTime.now();
        if(TimeRangeConstant.RANGE_AUTO.equals(this.getTimeRange())){
            query.put("start",this.getStart());
            query.put("end",this.getEnd());
        }else if(TimeRangeConstant.RANGE_MINUTE.equals(this.getTimeRange())){
            query.put("start",this.toTimestamp(now.minusMinutes(1L)));
            query.put("end",this.toTimestamp(now));
        }else if(TimeRangeConstant.RANGE_DAY.equals(this.getTimeRange())){
            query.put("start",this.toTimestamp(now.minusDays(1L)));
            query.put("end",this.toTimestamp(now));
        }else if(TimeRangeConstant.RANGE_WEEK.equals(this.getTimeRange())){
            query.put("start",this.toTimestamp(now.minusWeeks(1L)));
            query.put("end",this.toTimestamp(now));
        }else{
            query.put("start",this.toTimestamp(now.minusHours(1L)));
            query.put("end",this.toTimestamp(now));
        }

        Long start = (Long)query.get("start");
        Long end = (Long)query.get("end");
        Long delta = end - start;
        //如果超过了一千个点，则最多取一千个点
        query.put("step",this.getStep());
        if(delta/this.getStep()>TimeRangeConstant.MAX_POINT)
            query.put("step",delta/TimeRangeConstant.MAX_POINT);
        //如果小于60个点，则步长为1s
        if(delta/this.getStep()<TimeRangeConstant.MIN_POINT)
            query.put("step",1);

        if(timeout!=null){
            query.put("timeout",this.getTimeout());
        }
        return query;
    }

    public void buildPromQL(String promQL,Map<String,Object> params){
        if(StringUtils.isNotEmpty(this.getLabels())){
            for(String key : this.getLabels().keySet()){
                promQL = promQL.replaceAll(key,this.getLabels().get(key));
            }
        }
        params.put("query",promQL);
    }

    public Long toTimestamp(LocalDateTime date){
        return date.atZone(ZoneId.systemDefault()).toInstant().getEpochSecond();
    }

    public String getTimeRange() {
        return timeRange;
    }

    public void setTimeRange(String timeRange) {
        this.timeRange = timeRange;
    }

    public String getQuery() {

        return query;
    }

    public void setQuery(String query) {
        this.query = query;
    }

    public Long getStart() {
        return start;
    }

    public void setStart(Long start) {
        this.start = start;
    }

    public Long getEnd() {
        return end;
    }

    public void setEnd(Long end) {
        this.end = end;
    }

    public Long getStep() {
        return step;
    }

    public void setStep(Long step) {
        this.step = step;
    }

    public Long getTimeout() {
        return timeout;
    }

    public void setTimeout(Long timeout) {
        this.timeout = timeout;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public Map<String, String> getLabels() {
        return labels;
    }

    public void setLabels(Map<String, String> labels) {
        this.labels = labels;
    }
}
